package cn.shujuhai.qtadmin.platform.kit;

import java.io.IOException;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.hutool.core.util.ObjectUtil;

/**
 * Describe: Servlet 工具类
 * 
 * @author dch CreateTime: 2019/10/23
 */

public class ServletUtil {

    public static final String ADDR = "0:0:0:0:0:0:0:1";
    public static final String FIREFOX = "Firefox";
    public static final String CHROME = "Chrome";
    public static final String TRIDENT = "Trident";
    public static final String WINDOWS = "windows";
    public static final String MAC = "mac";
    public static final String X_11 = "x11";
    public static final String ANDROID = "android";
    public static final String IPHONE = "iphone";

    private ServletUtil() {

    }

    /**
     * Describe: 获取 HttpServletRequest 对象 Param null Return HttpServletRequest
     */
    public static @NotNull HttpServletRequest getRequest() {
        ServletRequestAttributes servletRequestAttributes =
            (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        return Objects.requireNonNull(servletRequestAttributes).getRequest();
    }

    /**
     * Describe: 获取 HttpServletResponse 对象 Param null Return HttpServletResponse
     */
    public static @Nullable HttpServletResponse getResponse() {
        if (ObjectUtil.isNotEmpty(RequestContextHolder.getRequestAttributes())) {
            ServletRequestAttributes servletRequestAttributes =
                (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
            if (ObjectUtil.isNotEmpty(servletRequestAttributes)) {
                return Objects.requireNonNull(servletRequestAttributes).getResponse();
            }
        }
        return null;
    }

    /**
     * Describe: 获取 HttpServletSession 对象 Param null Return HttpServletSession
     */
    public static @Nullable HttpSession getSession() {
        if (ObjectUtil.isNotEmpty(RequestContextHolder.getRequestAttributes())) {
            ServletRequestAttributes servletRequestAttributes =
                (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
            assert servletRequestAttributes != null;
            return servletRequestAttributes.getRequest().getSession();
        }
        return null;
    }

    /**
     * Describe: 判断是否为 Ajax 请求 Param null Return HttpServletSession
     */
    public static @NotNull Boolean isAjax(@NotNull HttpServletRequest request) {
        String requestType = request.getHeader("X-Requested-With");
        return "XMLHttpRequest".equals(requestType);
    }

    /**
     * Describe: Response 对象写出数据 Param: msg 消息数据 Return null
     */
    public static void write(String msg) throws IOException {
        if (ObjectUtil.isNotEmpty(getResponse())) {
            HttpServletResponse response = getResponse();
            if (ObjectUtil.isNotEmpty(response)) {
                assert response != null;
                response.setHeader("Content-type", "application/json;charset=UTF-8");
                response.setCharacterEncoding("UTF-8");
                response.getWriter().write(msg);
            }
        }
    }

    /**
     * 获取查询参数
     */
    public static String getQueryParam() {
        return getRequest().getQueryString();
    }

    /**
     * 获取请求地址
     */
    public static String getRequestUri() {
        return getRequest().getRequestURI();
    }

    /**
     * 获取客户端地址
     */
    public static String getRemoteHost() {
        String remoteHost = getRequest().getRemoteHost();
        if (ADDR.equals(remoteHost)) {
            remoteHost = "127.0.0.1";
        }
        return remoteHost;
    }

    /**
     * 获取当前请求方法
     */
    public static String getMethod() {
        return getRequest().getMethod();
    }

    /**
     * 获取请求头
     */
    public static String getHeader(String name) {
        return getRequest().getHeader(name);
    }

    /**
     * 获取 UserAgent
     */
    public static String getAgent() {
        return getHeader("User-Agent");
    }

    /**
     * 获取浏览器类型
     */
    public static @NotNull String getBrowser() {
        String userAgent = getAgent();
        if (userAgent.contains(FIREFOX)) {
            return "火狐浏览器";
        } else if (userAgent.contains(CHROME)) {
            return "谷歌浏览器";
        } else if (userAgent.contains(TRIDENT)) {
            return "IE 浏览器";
        } else {
            return "你用啥浏览器";
        }
    }

    /**
     * 获取浏览器类型
     */
    public static @NotNull String getSystem() {
        String userAgent = getAgent();
        if (userAgent.toLowerCase().contains(WINDOWS)) {
            return "Windows";
        } else if (userAgent.toLowerCase().contains(MAC)) {
            return "Mac";
        } else if (userAgent.toLowerCase().contains(X_11)) {
            return "Unix";
        } else if (userAgent.toLowerCase().contains(ANDROID)) {
            return "Android";
        } else if (userAgent.toLowerCase().contains(IPHONE)) {
            return "IPhone";
        } else {
            return "UnKnown, More-Info: " + userAgent;
        }
    }

}
